<template>
  <div class="app-container">
    <tree-table :data="data" :evalFunc="func" :columns="columns" :evalArgs="args" :expandAll="expandAll" border>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="success" size="mini" @click="handleCreate(scope.row)">{{'新增'}}</el-button>
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">{{'编辑'}}</el-button>
          <el-button v-if="scope.row.status!='deleted'" size="mini" type="danger" @click="message(scope.row,'deleted')">{{'删除'}}</el-button>
        </template>
      </el-table-column>
    </tree-table>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="100px" style='width: 400px; margin-left:100px;'>
        <el-form-item :label="'名称'" prop="username">
          <el-input v-model="temp.username"></el-input>
        </el-form-item>
        <el-form-item :label="'类型'" prop="username">
          <el-input v-model="temp.username"></el-input>
        </el-form-item>
        <el-form-item :label="'URL路径'" prop="username">
          <el-input v-model="temp.username"></el-input>
        </el-form-item>
        <el-form-item :label="'权限字符串'" prop="username">
          <el-input v-model="temp.username"></el-input>
        </el-form-item>
        <el-form-item :label="'排序'" prop="username">
          <el-input v-model="temp.username"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{$t('table.cancel')}}</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">{{$t('table.confirm')}}</el-button>
        <el-button v-else type="primary" @click="updateData">{{$t('table.confirm')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
/**
  Auth: Lei.j1ang
  Created: 2018/1/19-14:54
*/
import treeTable from '@/components/TreeTable'
import treeToArray from './customEval'

export default {
  name: 'customTreeTableDemo',
  components: { treeTable },
  data() {
    return {
      func: treeToArray,
      expandAll: false,
      dialogStatus: '',
      dialogFormVisible: false,
      textMap: {
        update: '编辑',
        create: '新建'
      },
      temp: {
        name: undefined,
        phone: 1,
        password: '',
        referee: '',
        belong: '',
        level: ''
      },
      rules: {
        username: [{ required: true, message: 'rolename is required', trigger: 'blur' }]
      },
      columns: [
        {
          text: '名称',
          value: 'event',
          width: 300
        },
        {
          text: 'URL路径',
          value: 'timeLine'
        },
        {
          text: '权限字符串',
          value: 'comment'
        },
        {
          text: '序号',
          value: 'id'
        }
      ],
      data:
        {
          id: 1,
          event: '模块1',
          timeLine: 100,
          comment: 'user:*',
          children: [
            {
              id: 2,
              event: '模块2',
              timeLine: 10,
              comment: 'user:*'
            },
            {
              id: 3,
              event: '模块3',
              timeLine: 90,
              comment: 'user:*',
              children: [
                {
                  id: 4,
                  event: '模块4',
                  timeLine: 5,
                  comment: 'user:*'
                },
                {
                  id: 5,
                  event: '模块5',
                  timeLine: 10,
                  comment: 'user:*'
                },
                {
                  id: 6,
                  event: '模块6',
                  timeLine: 75,
                  comment: 'user:*',
                  children: [
                    {
                      id: 7,
                      event: '模块7',
                      timeLine: 50,
                      comment: 'user:*',
                      children: [
                        {
                          id: 71,
                          event: '模块71',
                          timeLine: 25,
                          comment: 'xx'
                        },
                        {
                          id: 72,
                          event: '模块72',
                          timeLine: 5,
                          comment: 'xx'
                        },
                        {
                          id: 73,
                          event: '模块73',
                          timeLine: 20,
                          comment: 'xx'
                        }
                      ]
                    },
                    {
                      id: 8,
                      event: '模块8',
                      timeLine: 25,
                      comment: 'user:*'
                    }
                  ]
                }
              ]
            }
          ]
        },
      args: [null, null, 'timeLine']
    }
  },
  methods: {
    message(row) {
      this.$message.info(row.event)
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        status: 'published',
        type: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
          this.temp.author = 'vue-element-admin'
          this.dialogFormVisible = false
          // this.createArticle(this.temp).then(() => {
          //   this.list.unshift(this.temp)
          //   this.dialogFormVisible = false
          //   this.$notify({
          //     title: '成功',
          //     message: '创建成功',
          //     type: 'success',
          //     duration: 2000
          //   })
          // })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
          this.updateArticle(tempData).then(() => {
            for (const v of this.list) {
              if (v.id === this.temp.id) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, this.temp)
                break
              }
            }
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDelete(row) {
      this.$notify({
        title: '成功',
        message: '删除成功',
        type: 'success',
        duration: 2000
      })
      const index = this.list.indexOf(row)
      this.list.splice(index, 1)
    }
  }
}
</script>
